package com.person.controller;

import com.person.common.BaseContext;
import com.person.common.R;
import com.person.pojo.AddressBook;
import com.person.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     * 显示地址信息列表
     *
     */
    @GetMapping("list")
    public R<List<AddressBook>> list(){
        List<AddressBook> list = addressBookService.inquire(BaseContext.getCurrentId());
        return R.success(list);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public R<String> defaultAddress(@RequestBody AddressBook addressBook){
        log.info("设置默认地址id：{}",addressBook);
        addressBookService.setDefault(addressBook.getId());
        return R.success("修改成功");

    }

    /**
     * 支付时候回显默认地址
     */
    @GetMapping("/default")
    public R<AddressBook> showDefaultAddress(){
        return R.success(addressBookService.showDefaultAddress());
    }

    /**
     * 修改地址
     */
    //回显信息
    @GetMapping("/{id}")
    public R<AddressBook> showAddress(@PathVariable Long id){
        AddressBook addressBook = addressBookService.showAddress(id);
        return R.success(addressBook);
    }

    //修改并保存信息
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){

        addressBookService.updateAddress(addressBook);
        return R.success("保存成功");
    }
    /**
     * 删除地址
     */
    @DeleteMapping
    public R<String> remove(Long ids){
        addressBookService.remove(ids);
        return R.success("删除成功");
    }



}
